<template>
  <div>
    <a-modal v-model:open="modalOpen" title="发货" width="50%" :footer="null">
      <a-space direction="vertical" style="width: 100%" :size="16">
        <a-form
          :model="formData"
          labelAlign="left"
          :label-col="{ span: 3 }"
          :wrapper-col="{ span: 16 }"
        >
          <a-form-item
            label="物流编号"
            name="logisticsNo"
            :rules="[{ required: true, message: '物流编号不能为空!' }]"
          >
            <a-input
                v-model:value="formData.logisticsNo"
                placeholder="输入物流编号"
              />
          </a-form-item>

          <a-form-item
            label="物流公司"
            name="comCode"
            :rules="[{ required: true, message: '请选择物流公司' }]"
            v-if="logisticsList.length >= 2"
          >
            <a-select
              ref="select"
              v-model:value="formData.comCode"
              placeholder="请选择物流公司"
            >
              <a-select-option :value="item.comCode" v-for="(item,index) in logisticsList" :key="index">{{item.name}}</a-select-option>
            </a-select>
          </a-form-item>
        </a-form>
        <div class="modal-but-layout">
          <a-button type="primary" @click="handleSubmit">确认发货</a-button>
        </div>
      </a-space>
    </a-modal>
  </div>
</template>
<script setup>
import { ref } from 'vue'
import { sendToPlatformGood, identiylogistics} from '@/api/order'
import { PlusOutlined, LoadingOutlined } from '@ant-design/icons-vue'
import { message } from 'ant-design-vue';
const modalOpen = ref(false)

const formData = ref({
  id: '',
  logisticsNo: '',
  comCode: null,
  name: ''
})

const logisticsList = ref([])

const open = (id) => {
  formData.value.id = id
  // console.log(deliverId.value)
  modalOpen.value = true
}
const emits = defineEmits('refreshList')

const searchFunc = async () => { 
 
}

const handleSubmit = async () => {
  if (!formData.value.logisticsNo) {
    message.error('请输入快递单号')
    return
  }
  if (logisticsList.value == 0) {
    const data = await identiylogistics({logisticsNo: formData.value.logisticsNo})
    if(data.code === 200) {
      console.log('快递公司', data)
      logisticsList.value = data.data
      if (logisticsList.value.length == 0) { 
        message.error('暂未查到快递公司')
        return
      } else if (logisticsList.value.length == 1) { 
        message.info('发货成功')
        formData.value.comCode = logisticsList.value[0].comCode
        formData.value.name = logisticsList.value.filter(item => item.comCode == formData.value.comCode)[0].name
        submit()
      }
    }
  } else { 
    if (!formData.value.comCode) {
      message.error('请选择快递公司')
      return
    }
    submit()
  }
}

const submit = async () => { 
  console.log('formData.value', formData.value)
  const data = await sendToPlatformGood(formData.value)
  if(data.code === 200) {
    modalOpen.value = false
    emits('refreshList',true)
  }
}
defineExpose({ modalOpen, open })
</script>

<style lang="less">
.img-item {
  width: 140px;
  display: flex;
  flex-direction: column;
  align-items: center;

  .img-label {
    margin-top: 6px;
    color: #9599a5;
    font-size: 12px;
  }
}

.modal-but-layout {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
}
</style>
